// vue-router4.x：
import { createRouter, createWebHistory } from 'vue-router';
import store from "./element-store";

const routes = [
    {
        path: "/login",
        name: "login",
        component: () => import("../components/11element/login/Login")
    },
    {
        path: "/login/nopower",
        name: "nopower",
        component: () => import("../components/11element/login/NoPower")
    },
    {
        path: "/index",
        name: "index",
        component: () => import("../components/11element/login/Index"),
        children: [
            {
                path: "",
                name: "welcome",
                component: () => import("../components/11element/login/Welcome")
            },
            {
                path: "admin",
                name: "admin",
                component: () => import("../components/11element/admin/List")
            },
            {
                path: "content",
                name: "content",
                component: () => import("../components/11element/content/List")
            },
            {
                path: "banner",
                name: "banner",
                component: () => import("../components/11element/banner/banner-list")
            },
            {
                path: "updateSelf",
                name: "updateSelf",
                component: () => import("../components/11element/admin/UpdateSelf")
            }
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/login'
    }
]

// 创建路由对象
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})


// 全局前置守卫
router.beforeEach((to, from, next) => {
    // console.log("to.fullPath = ",to.fullPath);
    // console.log("to.fullPath.startsWith = ", to.fullPath.startsWith("/login"));
    if (to.fullPath == "/index" || to.fullPath.startsWith("/login")) {
        next();
        return;
    }
    // 判断有没有权限
    if (store.state.admin.pageURLs.some(v => v == to.fullPath)) {
        next();
    } else {
        next("/login/no_power");
    }
})

export default router;